package com.lan.controller.image;

import com.lan.utils.image.OCRUtil;
import net.sourceforge.tess4j.TesseractException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * <p>
 *  图像识别
 * </p>
 *
 * @author shadow
 * @since 2022-09-23
 */
@Controller
@RequestMapping("/image")
public class OCRController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OCRController.class);

    private static final String FILE_PATH =
            "E:\\MyWorkspace\\student\\student-sys\\src\\main\\resources\\static\\img\\imageTest\\子岚.jpg";

    /**
     * index.
     *
     * */
    @ResponseBody
    @GetMapping("index")
    public void index() {
        try {
            LOGGER.info("<<<<<<<<<<<<<<<<<<<< image start >>>>>>>>>>>>>>>>>>>>");
            LocalDateTime startTime = LocalDateTime.now();
            String ocrResult = OCRUtil.ocr(FILE_PATH);
            LOGGER.info("图片内容 : {} ", ocrResult);
            LOGGER.info("=============== 耗时 : {}ms", Duration.between(startTime, LocalDateTime.now()).abs().toMillis());
            LOGGER.info("<<<<<<<<<<<<<<<<<<<<<<<< --- >>>>>>>>>>>>>>>>>>>>>>>>\n");
        } catch (TesseractException e) {
            LOGGER.error("=============== 图像识别错误");
        }
    }

}
